// pages/cssCase/stepsAnt/index.js
const db = wx.cloud.database()
const Time = require('../../../utils/time/TimeUtil2')

Page({
  data: {
    text: "", //记录内容
    // 内容列表
    list: [{
      date: '',//年月日
      time: '',//时间
      content: '',//内容
      f:'',//在那边
      aut:'',//作者
      pinglun:[],//评论
    }, ],
    // 菜单列表
    menuList: [{
      name: "记录",
      src: 'cloud://womenlia-2gcdym6fb861b775.776f-womenlia-2gcdym6fb861b775-1331674006/线条小狗/xiantiao4.png',
      url: '',
    }, ],
    className: '', //样式名称    className: 'show',//样式名称
    hidden: true, //是否隐藏菜单 true 隐藏
  },

  //点击按钮
  handleClick() {
    let {
      hidden
    } = this.data;
    this.setData({
      hidden: !hidden,
      className: !hidden ? 'hide' : 'show'
    })
  },

  // 页面加载
  onLoad: function (options) {
    wx.showLoading({
      title: '加载',
      mask: true
    })
    this.getDataBius()
  },
  //查询数据库信息
  async getDataBius() {
    let value = wx.getStorageSync('autt').user
    let sj = await db.collection('duihuajilu').doc('ce6b953c678a64df04e09f714cb2313c').get()
    let sjk = sj.data
    let list = []
    console.log(sj.data);
    for (let i = sjk.arr.length; i > 0; i--) {
      let b = sjk.arr[i - 1]
      console.log(b);
      let a = {
        date: b.date[0],
        time: b.date[1],
        content: b.text,
        pinglun: b.pinglun,
        f: b.autid == value._id ? 'you' : 'zuo',
        aut:b.aut,
      }
      list.push(a)
    }
    this.setData({
      list: list,
      className: '',
      hidden: true,
      text:''
    })
    setTimeout(function () {
      wx.hideLoading()
    }, 2000)
  },

  // 点击提交记录
  async jilu(e) {
    //文本框输入内容
    console.log(e.detail.value.textarea);
    if (e.detail.value.textarea) {
      //获取当前时间
      let date = new Date();
      let sj = Time.formatDatetimexiegang(date)
      // 字符串截取
      let datetime = sj.split(" ")
      //获取作者
      let value = wx.getStorageSync('autt').user
      //数组对象
      let obj = {
        date: datetime, //评论时间
        pinglun: [], //评论默认为空
        text: e.detail.value.textarea, //记录内容
        autid: value._id, //记录id
        aut: value.aut, // 你还是我
      }
      wx.showLoading({
        title: '正在提交记录',
        mask: true
      })
      //添加到数据库
      let sjj = await db.collection('duihuajilu').doc('ce6b953c678a64df04e09f714cb2313c').update({
        data: {
          arr: db.command.push(obj)
        },
        success: res => {
          wx.showLoading({
            title: '成功',
            mask: true
          })
          this.getDataBius()
        }
      })

    } else {
      console.log("没有数字如");
      wx.showModal({
        title: '提示',
        content: '小笨蛋忘记记录了',
        complete: (res) => {
          if (res.cancel) {

          }

          if (res.confirm) {

          }
        }
      })
    }

  },














})